import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_riverpod_test/model/cart_item.dart';
import 'package:flutter_riverpod_test/model/product.dart';

typedef CartInfoListItemCallback = void Function(Product product, CartItem? cartItem, int change);

class CartInfoListItem extends ConsumerWidget {
  const CartInfoListItem({super.key, required this.product, required this.cartItem, required this.callback});

  static const double rowHeight = 50;

  final Product product;
  final CartItem? cartItem;
  final CartInfoListItemCallback callback;

  /// 加
  addCount() {
    callback(product, cartItem, 1);
  }

  /// 减
  reduceCount() {
    callback(product, cartItem, -1);
  }

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return Padding(
      padding: const EdgeInsets.only(left: 10),
      child: Column(
        children: [
          Expanded(
            child: Row(
              children: [
                Text(
                  product.name,
                  style: const TextStyle(
                      fontSize: 16,
                      fontWeight: FontWeight.bold
                  ),
                ),
                const Spacer(),
                SizedBox(
                  width: 120,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      IconButton(
                        icon: const Icon(
                          Icons.add_circle_rounded,
                          size: 20,
                        ),
                        onPressed:addCount,
                      ),
                      Text(
                        " ${cartItem?.count ?? 0}",
                        style: const TextStyle(
                            fontSize: 16,
                            fontWeight: FontWeight.bold
                        ),
                      ),
                      IconButton(
                        icon: const Icon(
                          Icons.remove_circle_rounded,
                          size: 20,
                        ),
                        onPressed:reduceCount,
                      )
                    ],
                  ),
                )
              ],
            ),
          ),
          const Padding(
            padding: EdgeInsets.symmetric(horizontal: 0),
            child: SizedBox(
              width: double.infinity,
              height: 0.5,
              child: DecoratedBox(
                decoration: BoxDecoration(
                    color: Colors.black12
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}
